package com.wuzhishan.news.controller;

import com.wuzhishan.news.pojo.BlogUser;
import com.wuzhishan.news.service.homeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/home")
public class HomeController {
    @Autowired
    private homeService homeService;

    @RequestMapping(value = {"","/","/index","/index.html"})
    public Object go_Index(HttpSession session){
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user==null){
            //未登录状态
            return "redirect:/login.html";
        }
        ModelAndView mv = new ModelAndView("/home/index");
        mv = homeService.notifiesCount(user.getId(),session,mv);
        return mv;
    }

    /**
     * 我的关注
     * @param page  //当前页面，默认页面大小在service层中设置
     * @param session
     * @return
     */
    @RequestMapping(value = "/follows")
    public Object follows(@RequestParam(defaultValue = "1") String page, HttpSession session){
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user==null){
            //未登录状态
            return "redirect:/login.html";
        }
        ModelAndView mv = new ModelAndView("/home/follows");
        mv = homeService.followers(user.getId(),page,mv);
        return mv;
    }

    /**
     * 我的粉丝
     * @param page
     * @param session
     * @return
     */
    @RequestMapping("/fans")
    public Object fans(@RequestParam(defaultValue = "1") String page, HttpSession session){
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user==null){
            //未登录状态
            return "redirect:/login.html";
        }
        ModelAndView mv = new ModelAndView("/home/fans");
        mv = homeService.fans(user.getId(),page,mv);
        return mv;
    }

    /**
     * 我的喜欢
     * @param page
     * @param session
     * @return
     */
    @RequestMapping("/favors")
    public Object favor(@RequestParam(defaultValue = "1") String page, HttpSession session){
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user==null){
            //未登录状态
            return "redirect:/login.html";
        }
        ModelAndView mv = new ModelAndView("/home/favor");
        mv = homeService.favor(user.getId(),page,mv);
        return mv;
    }

    /**
     * 我的通知
     * @param page
     * @param session
     * @return
     */
    @RequestMapping("/notifies")
    public Object notifies(@RequestParam(defaultValue = "1") String page, HttpSession session){
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user==null){
            //未登录状态
            return "redirect:/login.html";
        }
        session.setAttribute("totalCount",0);
        ModelAndView mv = new ModelAndView("/home/notifies");
        mv = homeService.notifies(user.getId(),page,mv);
        return mv;
    }
}
